// pages/member/giftCard/giftCard.js
let app = getApp();
import {list} from '../../../api/touristCard'
import {getInfo} from '../../../api/member'
import util from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    name:'',
    title:'',
    isShow:false,
    cardId:'',
    swiperHeight:800,
    myMemberId:null,
    getGift:[],
    myGift:[],
    currentTab: 0
  },
  loadMch(){
    var _this=this
    getInfo().then(res=>{
        this.data.myMemberId=res.data.member.id
        this.data.name=res.data.member.name
        setTimeout(()=>{
          _this.loadGift()
        },500)
    })
   
  },
  toUse(e){
    console.log(e)
    wx.navigateTo({
      // url: '/pages/member/touristCard/usetouristCard/index?id=' + e.currentTarget.dataset.id          
      url:'/pages/member/touristCard/usetouristCard/usetouristCard?id=' + e.currentTarget.dataset.id       
      })
  },

  DateDifference(faultDate,completeTime){

    var stime =new Date(faultDate).getTime();
    var etime = new Date(completeTime).getTime();
    var usedTime = etime - stime; 
    var days=Math.floor(usedTime/(24*3600*1000));
    var leave1=usedTime%(24*3600*1000);   
    var hours=Math.floor(leave1/(3600*1000));
    var leave2=leave1%(3600*1000);  
    var minutes=Math.floor(leave2/(60*1000));
    // var time = days + "天"+hours+"时"+minutes+"分";
    var time = days;
    return time;
  },




  loadGift(){
    var _this = this;
    if(this.data.currentTab==0){
      list({
        memberId:this.data.myMemberId,
        pageNum:this.data.pageNum
      }).then(res=>{
          res.data.data.forEach(item=>{
            // item.createDate=util.datetimeFormat(item.createDate)      
            item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)  
            _this.data.myGift.push(item)
              _this.setData({
              myGift:_this.data.myGift,
            })
          })
      })
  } else {
    list({
        receiverId:this.data.myMemberId,
        pageNum:this.data.pageNum
    }).then(res=>{
        res.data.data.forEach(item=>{
          item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)       
          _this.data.getGift.push(item)
          _this.setData({
              getGift:_this.data.getGift,
            })
        })
    })
  }
  },
  toGive(){
    wx.navigateTo({
      url: '/pages/member/givetouristCard/givetouristCard'
    })
  },
  giveClick(e){
      this.data.cardId=e.currentTarget.dataset.id
      console.log( this.data.cardId)
      this.setData({
        cardId:this.data.cardId
      })

  },
  reachBottom(){
    this.data.pageNum+=1
    if(this.data.currentTab==0){
      list({
        memberId:this.data.myMemberId,
        pageNum:this.data.pageNum
      }).then(res=>{
          res.data.data.forEach(item=>{
            item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)        
              this.data.myGift.push(item)
            this.setData({
              myGift:this.data.myGift,
            })
          })
      })
    }else{
      list({
        receiverId:this.data.myMemberId,
        pageNum:this.data.pageNum
      }).then(res=>{
          res.data.data.forEach(item=>{
            item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)       
              this.data.getGift.push(item)
            this.setData({
              getGift:this.data.getGift,
            })
          })
      })
    }
  },
  bindChange(e) {
    this.data.pageNum=1
    this.data.myGift=[]
    this.data.getGift=[]
    var that = this;
    if(e.detail.current==0){
      list({
        memberId:this.data.myMemberId,
        pageNum:this.data.pageNum
      }).then(res=>{
          res.data.data.forEach(item=>{
            item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)        
              this.data.myGift.push(item)
            this.setData({
              myGift:this.data.myGift,
            })
          })
      })
    }else{
      list({
        receiverId:this.data.myMemberId,
        pageNum:this.data.pageNum
      }).then(res=>{
          res.data.data.forEach(item=>{
            item.createDate=this.DateDifference(item.validStartDate,item.validEndDate)        
              this.data.getGift.push(item)
            this.setData({
              getGift:this.data.getGift,
            })
          })
      })
    }

    that.setData({
            swiperHeight:that.data.swiperHeight,
            currentTab: e.detail.current
    });
},
  navTo() {
    let p = getCurrentPages().length;
    if (p <= 1) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  
  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })

    }
  },
  toCardInfo(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/member/touristCard/cardInfo/cardInfo?id='+e.currentTarget.dataset.id
    })
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('-------------------------------')
    console.log(options)
    if(!util.isNull(options.type)){
      this.data.currentTab=options.type
      this.setData({
        currentTab:this.data.currentTab
      })
    }
    this.loadMch()
    var that = this;
    that.setNavSize();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (data) {
    return {
      title: this.data.name+'送您一张旅游卡卡快来领取',
      imageUrl: 'http://rzico.oss-cn-shenzhen.aliyuncs.com/img/bg_giftcard_mainbg.png' + '?x-oss-process=image/resize,m_fixed,h_500,w_500',
      desc: '分享页面的内容',
      path: '/pages/member/givetouristCard/givetouristCard?cardId=' +data.target.dataset.id+'&xuid=' + app.globalData.userInfo.id// 路径，传递参数到指定页面。
    }
  },
})